package com.neusoft.elm.view.impl;

import java.util.Scanner;
import java.util.regex.Pattern;

import com.neusoft.elm.dao.AdminDao;
import com.neusoft.elm.dao.impl.AdminDaoImpl;
import com.neusoft.elm.po.Admin;
import com.neusoft.elm.view.AdminView;

public class AdminViewImpl implements AdminView{
	
	private Scanner input = new Scanner(System.in);
	
	@Override
	public Admin login(){
		System.out.println("请输入管理员账户：");
		String adminName = input.next();
		System.out.println("请输入密码：");
		String password = input.next();
		
		AdminDao dao = new AdminDaoImpl();
		return dao.getAdminByNameByPass(adminName, password);
	}
	
	
	@Override
	public void updateAdminPassword(Admin admin) {
	    AdminDao dao = new AdminDaoImpl();

	    Scanner scanner = new Scanner(System.in);

	    System.out.println("请输入旧密码：");
	    String oldPassword = scanner.nextLine();

	    System.out.println("请输入新密码（密码规则：8~20位，包含大小写字母、数字、特殊字符（!@#$%^&*()_+））：");
	    String newPassword = scanner.nextLine();

	    System.out.println("请再次输入新密码：");
	    String confirmPassword = scanner.nextLine();

	   
	    Pattern pattern = Pattern.compile("^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[!@#$%^&*()_+])[a-zA-Z\\d!@#$%^&*()_+]{8,20}$");

	    if (!admin.getPassword().equals(oldPassword)) {
	        System.out.println("旧密码输入错误！");
	        return;
	    } else if (!newPassword.equals(confirmPassword)) {
	        System.out.println("两次输入密码不一致！");
	        return;
	    } else if (!pattern.matcher(newPassword).matches()) {
	        System.out.println("新密码不符合规则，请重新输入。");
	        return;
	    }

	    boolean success = dao.updateAdminPassword(admin.getAdminName(), newPassword);
	    if (success) {
	        System.out.println("密码修改成功！");
	    } else {
	        System.out.println("密码修改失败，请重试！");
	    }
	}
	
	
	@Override
	public void updateAdminName(Admin admin) {
		AdminDao dao = new AdminDaoImpl();
		
		System.out.println("\n请输入新管理员账户名：");
	    String newAdminName = input.next();
	    
	    boolean success = dao.updateAdminName(admin.getAdminName(), newAdminName);
	    if (success) {
	    	System.out.println("管理员账户名修改成功！");
	    } else {
	    	System.out.println("管理员账户名修改失败，请重试！");
	    }
	}

}
